﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YZZ
{
    #region 动态读取记录
    class AccountClass
    {
        public AccountClass(string token, string key, string signature, string other)
        {
            Access_token = token;
            AppKey = key;
            AppSignatureKey = signature;
            Refresh_token = other;
        }
        public AccountClass(string token, string key, string signature, string other, DateTime expireTime) : this(token, key, signature, other)
        {
            ExpireTime = new DateTime();
            if (expireTime !=null && expireTime != new DateTime())
                ExpireTime = expireTime;
            else
                ExpireTime = DateTime.Now.AddMinutes(-1);
        }
        public void UpdateAccessToken(string access_token) { Access_token = access_token; }
        public AccountClass Account() { return this; }
        public DateTime ExpireTime { get; private set; }
        public string Access_token { get; private set; }
        public string AppKey { get; private set; }
        public string AppSignatureKey { get; private set; }
        public string Refresh_token { get; private set; }
    }
    #endregion
    #region 静态信息储存器
    class AccountModel
    {
        static private string Access_token { get; set; }
        static private string AppKey { get; set; }
        static private string AppSignatureKey { get; set; }
        static private string Refresh_token { get;  set; }
        static private DateTime ExpireTime { get; set; }
        internal AccountClass GetAcount()
        {
            return new AccountClass(Access_token, Config.Get("SerialNumber"), Config.Get("Word"), Refresh_token, ExpireTime);
        }

        internal void UpdateAccessToken(string access_token, string expires_in)
        {
            double seconds = 0;
            ExpireTime = new DateTime();
            ExpireTime = DateTime.Now;
            double.TryParse(expires_in, out seconds);
            if (seconds > 0)
                ExpireTime = DateTime.Now.AddSeconds(seconds);
            Access_token = access_token;
        }
        internal void UpdateAccessToken(string access_token, Func<AccountClass> account)
        {
            Access_token = access_token;
        }
    }
    #endregion
}
